package com.chagee.channel.server.config;

import com.chagee.channel.server.enums.BusinessEnum;
import com.chagee.channel.server.event.model.OssCustomBO;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author: chang.jiawei
 * @date: 2024/03/13
 */
@Data
@Configuration
@Component
@RefreshScope
@ConfigurationProperties(prefix = "aliyun.oss")
public class OssCustomConfig {

    private Map<String, OssCustomBO> custom;
    @Getter
    @Setter
    private List<String> videoFormats;

    public Optional<OssCustomBO> fetchCustomConfig(BusinessEnum businessEnum) {
        Optional<BusinessEnum> paramOptional = Optional.ofNullable(businessEnum);
        if (!paramOptional.isPresent()) {
            return Optional.empty();
        }
        return Optional.ofNullable(custom.get(businessEnum.getType()));
    }
    public String fetchPrefix(BusinessEnum businessEnum) {
        Optional<OssCustomBO> ossCustomBO = fetchCustomConfig(businessEnum);
        return ossCustomBO.map(customBO -> Optional.ofNullable(customBO.getPrefix()).orElse(StringUtils.EMPTY)).orElse(StringUtils.EMPTY);
    }
}
